package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.union.cross.CrossUnionActivityLockRank;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionActivityLockRankMgr extends TempMgr {

    static Map<Integer, Map<Integer, List<CrossUnionActivityLockRank>>> rankLockMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        rankLockMap = DaoHelper.getCrossUnionActivityLockRankDaoImpl().getCrossUnionActivityLockRank(DateHelper.getCurrentSecond());
        return true;
    }

    @Override
    public boolean save() {

        for (Map.Entry<Integer, Map<Integer, List<CrossUnionActivityLockRank>>> entry : rankLockMap.entrySet()) {
            for (Map.Entry<Integer, List<CrossUnionActivityLockRank>> lockRankEntry : entry.getValue().entrySet()) {
                List<CrossUnionActivityLockRank> rankList = lockRankEntry.getValue();
                for (int i = 0; i < rankList.size(); i++) {
                    CrossUnionActivityLockRank rank = rankList.get(i);
                    if(rank.isInsertOption()){
                        DaoHelper.getCrossUnionActivityLockRankDaoImpl().add(rank);
                    }
                }
            }
        }
        return true;
    }

    public static Map<Integer, List<CrossUnionActivityLockRank>> getActivityMap(int activityId){
        Map<Integer, List<CrossUnionActivityLockRank>> map = rankLockMap.get(activityId);
        if(map == null){
            synchronized (rankLockMap){
                map = rankLockMap.get(activityId);
                if(map == null){
                    map = new ConcurrentHashMap<>();
                    rankLockMap.put(activityId, map);
                    return map;
                }
            }
        }
        return map;
    }

    public static CrossUnionProto.CrossUnionActivityLockRankResp.Builder getLockRankList(long serverId, int activityId, int activityEndTime, int rankNum) {
        CrossUnionProto.CrossUnionActivityLockRankResp.Builder respMsg = CrossUnionProto.CrossUnionActivityLockRankResp.newBuilder();
        respMsg.setActivityId(activityId);
        Map<Integer, List<CrossUnionActivityLockRank>> map = getActivityMap(activityId);
        int groupId = CrossUnionServerGroupMgr.getGroupIdByServerId(serverId);
        List<CrossUnionActivityLockRank> rankList = map.get(groupId);
        if(rankList == null){
            List<CrossUnionInfo> unionList = CrossUnionMgr.getUnionListInSameGroup(serverId);
            synchronized (map){
                rankList = map.get(groupId);
                if(rankList == null){
                    rankList = new ArrayList<>();
                    unionList.sort((o1, o2) -> o2.getTotalEarnSpeed().compareTo(o1.getTotalEarnSpeed()));
                    Set<Long> serverSet = CrossUnionServerGroupMgr.getGroupServer(groupId);
                    int rankNum2 = (rankNum + 10) * serverSet.size();
                    int listSize = unionList.size();
                    CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroup(groupId);
                    for (int i = 0; i < listSize && i < rankNum2; i++) {
                        CrossUnionInfo unionInfo = unionList.get(i);
                        CrossUnionActivityLockRank lockRank = new CrossUnionActivityLockRank();
                        lockRank.setActivityId(activityId);
                        lockRank.setEndTime(activityEndTime);
                        lockRank.setRank(i+1);
                        lockRank.setUnionUid(unionInfo.getUnionUid());
                        lockRank.setUnionBaseInfo(CrossUnionMgr.parseUnionBaseInfo(unionInfo));
                        lockRank.setGroupId(groupId);
                        lockRank.setInsertOption();
                        if(serverGroup != null) {
                            lockRank.setServerList(serverGroup.getServerList());
                        }else {
                            lockRank.getServerList().add(serverId);
                        }
                        rankList.add(lockRank);
                    }
                    map.put(groupId, rankList);
                }
            }
        }
        for (int i = 0; i < rankList.size() && i < rankNum; i++) {
            CrossUnionActivityLockRank lockRank = rankList.get(i);
            CrossUnionProto.CrossUnionActivityLockRankTemp.Builder builder = CrossUnionProto.CrossUnionActivityLockRankTemp.newBuilder();
            builder.setUnionUid(lockRank.getUnionUid());
            builder.setUnionBase(UnionBasePb.parseUnionBaseTempMsg(lockRank.getUnionBaseInfo()));
            respMsg.addRank(builder);
        }

        return respMsg;
    }

    @Override
    public boolean stop() {
        return true;
    }



}
